<template>
  <div class="inputInput">
    <el-form :model="formData" size="mini" ref="formData">
      <el-form-item>
        <ConditionsType v-model="formData.parentParam" @change="valueChange" ref="parentConditionsType"></ConditionsType>
      </el-form-item>
      <!-- 上级字段选择函数后当前选项隐藏 -->
      <el-form-item v-if="type !== '1'">
        <div class="flex">
          <div class="mr-5 fontMini">"当前选项"</div>
          <el-select class="flex1" v-model="formData.currentShow">
            <el-option v-for="item in currentShowList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item v-if="formData.currentShow === 'showAnd'">
        <ConditionsType v-model="formData.currentParam" :titleList="titleList" @change="valueChange" ref="currentConditionsType"></ConditionsType>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
const currentShowList = [
  { label: '显示', value: 'show' },
  { label: '隐藏', value: 'hidden' },
  { label: '显示且', value: 'showAnd' }
]
import { mapState } from 'vuex'
import ConditionsType from './conditionsType'
export default {
  name: 'inputInput',
  model: {
    prop: 'value',
    event: 'change'
  },
  props: {
    value: Object
  },
  components: {
    // CodeMirrorJs,
    ConditionsType
  },
  data() {
    return {
      currentShowList,
      formData: {},
      titleList: ['采用', '则需要满足', '条件']
    }
  },
  computed: {
    ...mapState('pageData', ['activeFormData']),
    type() {
      return this.value?.parentParam?.type
    }
  },
  watch: {
    value: {
      handler(val) {
        this.formData = JSON.stringify(val) === '{}' ? this.formDataInit() : val
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    formDataInit() {
      return {
        currentShow: 'show',
        parentParam: {},
        currentParam: {}
      }
    },
    valueChange() {
      this.$emit('change', this.formData)
    },
    verfiyForm() {
      return new Promise(async resolve => {
        const parentRes = await this.$refs.parentConditionsType.verfiyForm()
        if (this.formData.currentShow === 'showAnd') {
          const currentRes = await this.$refs.currentConditionsType.verfiyForm()
          if (parentRes === 'success' && currentRes === 'success') {
            resolve('success')
          } else {
            resolve('error')
          }
        } else {
          if (parentRes === 'success') {
            resolve('success')
          } else {
            resolve('error')
          }
        }
      })
    }
  }
}
</script>